<template>
  <div v-loading="loading" class="app-container">
    <el-card class="tree-card" shadow="never">
      <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
      <!-- 树形结构 -->
      <el-tree :data="departs" :props="defaultProps" default-expand-all>
        <template slot-scope="{ data }">
          <tree-tools :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
        </template>
      </el-tree>
    </el-card>
    <addDepts ref="addDepts" :tree-node="node" :show-dialog.sync="showDialog" @addDepts="getDepartments" />
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import addDepts from './components/add-depts.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
export default {
  components: { treeTools, addDepts },
  data() {
    return {
      company: { },
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      showDialog: false, // 显示窗体
      node: null // 记录当前点击node的节点
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const res = await getDepartments()
      console.log(res)
      this.company = { name: res.companyName, manager: res.companyManager || '负责人', id: '' }
      this.departs = tranListToTreeData(res.depts, '')
      this.loading = false
    },
    async addDepts(node) {
      this.showDialog = true // 显示弹层
      // 因为node是当前的点击的部门， 此时这个部门应该记录下来,
      this.node = node
    },
    editDepts(node) {
      this.showDialog = true // 首先打开弹层
      this.node = node // 赋值操作的节点
      // 父级调用 调用子组件的方法
      this.$refs.addDepts.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
